<script setup>
import { ref } from 'vue'
import { addArticleCate, editArticleCate } from '@/api/article.js'
defineProps({
  title: {
    type: String,
    required: true
  }
})

const dialogVisible = ref(false)
const data = ref({
  cate_name: '',
  cate_alias: ''
})
const open =  (row) => {
  data.value = row
  console.log(data.value)
  dialogVisible.value = true
}
defineExpose({
  open
})
const emit = defineEmits(['success'])

const goSubmit = async () => {
  // 如果id存在，则是编辑，否则是新增
  data.value.id ? await editArticleCate(data.value) : await addArticleCate(data.value)
  data.value.id ? ElMessage.success('编辑成功！！') : ElMessage.success('新增成功！！')
  dialogVisible.value = false
  emit('success')
}


const rules = {
  cate_name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' },
    {
      pattern: /^\S{1,10}$/,
      message: '分类名必须是1-10位的非空字符',
      trigger: 'blur'
    }
  ],
  cate_alias: [
    { required: true, message: '请输入分类别名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{1,15}$/,
      message: '分类别名必须是1-15位的字母数字',
      trigger: 'blur'
    }
  ]
}
</script>

<template>
  <el-dialog v-model="dialogVisible" :title="title" width="30%">
    <div>
      <el-form ref="formRef" style="max-width: 600px" :model="data" label-width="auto" :rules="rules">
        <el-form-item prop="cate_name" label="分类名称">
          <el-input v-model="data.cate_name" />
        </el-form-item>
        <el-form-item prop="cate_alias" label="分类别名">
          <el-input v-model="data.cate_alias" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
    <span class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="goSubmit"> 提交 </el-button>
    </span>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>
